package com.zhu.cloudspring.service;

import com.zhu.cloudspring.Repository.OrderRepository;
import com.zhu.cloudspring.entity.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

@Service
public class OrderService {

    @Autowired
    private OrderRepository orderRepository;

    @Autowired
    private RabbitMQSender rabbitMQSender;

    @Autowired
    private SmsService smsService;

    public Order createOrder(int userId, String phoneNumber, BigDecimal orderAmount, String consignee) {
        Order order = new Order();
        order.setUserId(userId);
        order.setPhoneNumber(phoneNumber);
        order.setOrderAmount(orderAmount);
        order.setCreatedAt(new Date());
        order.setUpdatedAt(new Date());
        Order savedOrder = orderRepository.save(order);
        rabbitMQSender.sendOrderMessage(savedOrder, consignee);
        return savedOrder;
    }
}
